#!/usr/bin/env python

import os
import subprocess
import sys

from lib.config import PLATFORM
from lib.util import execute, rm_rf, scoped_env


SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))

LINUX_DEPS = [
  'libdbus-1-dev',
  'libgconf2-dev',
  'libgnome-keyring-dev',
  'libgtk2.0-dev',
  'libnotify-dev',
  'libnss3-dev',
  'libxtst-dev',
  'gcc-multilib',
  'g++-multilib',
]

LINUX_DEPS_ARM = [
  'libc6-dev-armhf-cross',
  'linux-libc-dev-armhf-cross',
  'g++-arm-linux-gnueabihf',
]


def main():
  os.environ['CI'] = '1'

  if os.environ.has_key('JANKY_SHA1'):
    setup_nodenv()

  # Ignore the CXX and CC env in CI.
  try:
    del os.environ['CC']
    del os.environ['CXX']
  except KeyError:
    pass

  target_arch = 'x64'
  if os.environ.has_key('TARGET_ARCH'):
    target_arch = os.environ['TARGET_ARCH']

  is_travis = (os.getenv('TRAVIS') == 'true')
  if is_travis and PLATFORM == 'linux':
    print 'Setup travis CI'
    execute(['sudo', 'apt-get', 'update'])
    deps = LINUX_DEPS
    if target_arch == 'arm':
      deps += LINUX_DEPS_ARM
    execute(['sudo', 'apt-get', 'install'] + deps)

    execute(['sh', '-e', '/etc/init.d/xvfb', 'start'])

  if PLATFORM == 'linux':
    os.environ['DISPLAY'] = ':99.0'

  # CI's npm is not reliable.
  npm = 'npm.cmd' if PLATFORM == 'win32' else 'npm'
  execute([npm, 'install', 'npm@2.12.1'])

  log_versions()
  # Add "./node_modules/.bin" to the beginning of $PATH, which will ensure
  # future "npm" invocations use the right version.
  node_bin_dir = os.path.join(SOURCE_ROOT, 'node_modules', '.bin')
  os.environ['PATH'] = os.path.pathsep.join([node_bin_dir,
                                             os.environ.get('PATH', '')])

  is_release = os.environ.has_key('ELECTRON_RELEASE')
  args = ['--target_arch=' + target_arch]
  if not is_release:
    args += ['--dev']
  run_script('bootstrap.py', args)

  if PLATFORM != 'win32':
    sys.stderr.write('\nRunning `npm run lint`\n')
    sys.stderr.flush()
    execute([npm, 'run', 'lint'])

  if is_release:
    run_script('build.py', ['-c', 'R'])
    run_script('create-dist.py')
    run_script('upload.py')
  else:
    run_script('build.py', ['-c', 'D'])
    if PLATFORM == 'win32' or target_arch == 'x64':
      run_script('test.py', ['--ci'])
      run_script('verify-ffmpeg.py')


def run_script(script, args=[]):
  sys.stderr.write('\nRunning ' + script +'\n')
  sys.stderr.flush()
  script = os.path.join(SOURCE_ROOT, 'script', script)
  subprocess.check_call([sys.executable, script] + args)


def log_versions():
  sys.stderr.write('\nnode --version\n')
  sys.stderr.flush()
  subprocess.call(['node', '--version'])

  sys.stderr.write('\nnpm --version\n')
  sys.stderr.flush()
  npm = 'npm.cmd' if PLATFORM == 'win32' else 'npm'
  subprocess.call([npm, '--version'])


def setup_nodenv():
  if os.path.isdir('/usr/local/share/nodenv'):
    nodenv_root = os.path.join(os.environ['HOME'], '.nodenv')
    os.environ['NODENV_ROOT'] = nodenv_root
    os.environ['PATH'] = nodenv_root + '/bin:' + nodenv_root + '/shims:' + os.environ['PATH']
    os.environ['NODENV_VERSION'] = 'v4.5.0'
    subprocess.check_call(['/usr/local/share/nodenv/bin/nodenv', 'install', os.environ['NODENV_VERSION']])
    subprocess.check_call(['/usr/local/share/nodenv/bin/nodenv', 'rehash'])


if __name__ == '__main__':
  sys.exit(main())
